package com.ankicoo.common.service.impl;

import com.ankicoo.common.entity.CommCityPO;
import com.ankicoo.common.mapper.CommCityMapper;
import com.ankicoo.common.model.CityKindEnum;
import com.ankicoo.common.model.CityList;
import com.ankicoo.common.response.ResponseBody;
import com.ankicoo.common.service.CommCityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author : QCYANG
 * @date : 2021/11/24 18:02
 */
@Service
public class CommCityServiceImpl implements CommCityService {
    @Autowired
    private CommCityMapper cityMapper;

    @Override
    public List<CommCityPO> findCityListByLevel(Integer level){
        Example example = new Example(CommCityPO.class);
        example.createCriteria().andEqualTo("kindId",level);
        return  cityMapper.selectByExample(example);
    }

    @Override
    public List<CityList> findFullCityListByLevel(Integer level){
        List<CityList> collect = findCityListByLevel(level)
                .stream()
                .map(this::fillSubCityInfo)
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public CityList fillSubCityInfo(CommCityPO city){
        CityList cityList = new CityList();
        BeanUtils.copyProperties(city,cityList);

        int level = Integer.parseInt(city.getKindId());
        if (level==CityKindEnum.STREET.getCode()){
            return cityList;
        }
        List<CityList> subCityList =
                findSubCityListById(city.getCityId()).stream()
                        .map(this::fillSubCityInfo)
                        .collect(Collectors.toList());
        cityList.setSubCity(subCityList);
        return cityList;
    }

    @Override
    public List<CommCityPO> findSubCityListById(String cityId){
        Example example = new Example(CommCityPO.class);
        example.createCriteria().andEqualTo("upId",cityId);
        return cityMapper.selectByExample(example);
    }



}
